package com.xilada.xldutils.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.xilada.xldutils.R;
import com.xilada.xldutils.adapter.HeaderAndFooterRecyclerAdapter;
import com.xilada.xldutils.utils.DensityUtil;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

/**
 *
 * Created by liaoxiang on 16/1/18.
 */
public abstract class VerticalLinearRecyclerViewFragment extends BaseLazyFragment{

    protected RecyclerView mRecyclerView;
    protected SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView.ItemDecoration defaultItemDecoration;
    private int lastVisibleItem = 0;
    private boolean isLoadMore = false;
    private TextView loadMoreView;
    protected int DEFAULT_MARGIN_DIVIDER = 0;
    protected int DEFAULT_DIVIDER = 1;

    protected abstract RecyclerView.Adapter setAdapter();
    /**
     * 下拉刷新
     */
    protected abstract void pullDownRefresh();

    /**
     * 加载更多
     */
    protected abstract void loadMore();

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_vertical_linear_recyclerview;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView = mHolder.bind(R.id.mRecyclerView);
        mSwipeRefreshLayout = mHolder.bind(R.id.mSwipeRefreshLayout);

        mRecyclerView.setHasFixedSize(true);
        defaultItemDecoration = new HorizontalDividerItemDecoration.Builder(context)
                .colorResId(R.color.dividing_line_color)
                .marginResId(R.dimen.activity_horizontal_margin, R.dimen.activity_horizontal_margin)
                .size(1)
                .build();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
        mRecyclerView.addOnScrollListener(onScrollListener);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                setRefreshing(true);
                //重置可见item值。避免不足一页刷新后无法上拉加载
                pullDownRefresh();
            }
        });
        if (setAdapter() != null) {


            RecyclerView.Adapter adapter = setAdapter();

            if (adapter instanceof HeaderAndFooterRecyclerAdapter){
                loadMoreView = new TextView(context);
                loadMoreView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//                loadMoreView.setHeight(DensityUtil.dip2px(this,48));
                loadMoreView.setBackgroundColor(Color.WHITE);
                loadMoreView.setText("载入更多...");
                loadMoreView.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
                loadMoreView.setTextColor(getResources().getColor(R.color.textColor));
                loadMoreView.setGravity(Gravity.CENTER);
                loadMoreView.setPadding(0, DensityUtil.dip2px(context,16),0, DensityUtil.dip2px(getActivity(),16));
                ((HeaderAndFooterRecyclerAdapter)adapter).setFooterView(loadMoreView);
            }
            mRecyclerView.setAdapter(adapter);
        }
    }

    /**
     * listView滑动监听
     */
    private RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(RecyclerView view, int newState) {

            RecyclerView.Adapter adapter =view.getAdapter();
            resetLastVisibleItem();
            if (newState == RecyclerView.SCROLL_STATE_IDLE && adapter != null
                    && lastVisibleItem + 1 == adapter.getItemCount() && !mSwipeRefreshLayout.isRefreshing() && !isLoadMore) {
                if (adapter instanceof HeaderAndFooterRecyclerAdapter){

                    if (loadMoreView!=null ){
                        loadMoreView.setText("载入更多...");
                        loadMoreView.setVisibility(View.VISIBLE);
                    }
                    isLoadMore = true;
                    loadMore();
                }
            }
        }

        @Override
        public void onScrolled(RecyclerView view, int dx, int dy) {
        }
    };

    /**
     * 重新获取可见item的位置
     */
    private void resetLastVisibleItem(){
        RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
        if (layoutManager != null && layoutManager instanceof LinearLayoutManager) {
            lastVisibleItem = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
        }
    }

    /**
     * 设置刷新状态
     * @param refreshing 刷新状态
     */
    protected void setRefreshing(boolean refreshing){
        mSwipeRefreshLayout.setRefreshing(refreshing);
        this.isLoadMore = refreshing;
        if (loadMoreView!=null ){
            loadMoreView.setVisibility(View.GONE);
        }
    }

    protected void setLoadMoreText(CharSequence text){
        if (loadMoreView!=null){
            loadMoreView.setText(text);
            if (loadMoreView.getVisibility() != View.VISIBLE){
                loadMoreView.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * 设置分割线
     * @param itemDecoration 样式
     */
    protected void setItemDecoration(RecyclerView.ItemDecoration itemDecoration){
        if (itemDecoration == null){
            mRecyclerView.addItemDecoration(defaultItemDecoration);
            return;
        }
        if (defaultItemDecoration != null) {
            mRecyclerView.removeItemDecoration(defaultItemDecoration);
        }
        defaultItemDecoration = itemDecoration;
        mRecyclerView.addItemDecoration(defaultItemDecoration);
    }

    protected void setItemDecoration(int type) {
        HorizontalDividerItemDecoration itemDecoration;
        if (type == DEFAULT_MARGIN_DIVIDER){
            itemDecoration = new HorizontalDividerItemDecoration.Builder(context)
                    .margin(DensityUtil.dip2px(context,16))
                    .size(1)
                    .color(this.getResources().getColor(R.color.dividing_line_color))
                    .build();

        }else {
            itemDecoration = new HorizontalDividerItemDecoration.Builder(context)
                    .size(1)
                    .color(this.getResources().getColor(R.color.dividing_line_color))
                    .build();
        }
        setItemDecoration(itemDecoration);
    }
}
